package edu.rit.drh4818.raytracing.util;

import java.awt.Color;

import edu.rit.drh4818.raytracing.object.SceneObject;

/**
 * The information which represents an intersection of a ray and an object.
 * 
 * @author Darren Headrick
 *
 */
public class Intersection 
{
	/**
	 * Constant for no intersection found.
	 */
	public static final Intersection NONE = new Intersection( Double.MAX_VALUE, null, null );
	
	/**
	 * 
	 */
	private Ray normal;
	
	/**
	 * Value along ray where intersection.
	 */
	public final double t;
	
	/**
	 * Object of intersection.
	 */
	public final SceneObject obj;
	
	/**
	 * Original Ray.
	 */
	public final Ray ray;
	
	
	/**
	 * Create a new intersection.
	 * 
	 * @param t t value of intersection.
	 * @param obj Object which intersects ray.
	 * @param ray Original ray.
	 */
	public Intersection( double t, SceneObject obj, Ray ray )
	{
		
		if( t < 0 )
		{
			throw new IllegalArgumentException( "T value is negative" );
		}
		
		this.t = t;
		this.obj = obj;
		this.ray = ray;
	}
	
	/**
	 * Get the normal, and store result to prevent multiple calculations.
	 * @return
	 */
	public Ray getNormal()
	{
		if( normal == null )
		{
			normal = obj.getNormal( this );
		}
		return normal;
	}
	
	/**
	 * Get color at intersection.
	 * @return color at intersection.
	 */
	public Color getColor(int depth)
	{
		if( this == Intersection.NONE )
		{
			return Scene.instance.background;
		}
		
		return Scene.instance.sceneShader.getColor( this, depth );
	}
	
	/**
	 * Get the x coordinate of intersection.
	 * @return x coord of intersection.
	 */
	public double getX()
	{
		return ray.getX( t );
	}
	
	/**
	 * Get the y coordinate of intersection.
	 * @return y coord of intersection.
	 */
	public double getY()
	{
		return ray.getY( t );
	}
	
	/**
	 * Get the z coordinate of intersection.
	 * @return z coord of intersection.
	 */
	public double getZ()
	{
		return ray.getZ( t );
	}
}